package com.ja.openchat.views;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;

import com.blankj.utilcode.util.SizeUtils;
import com.ja.openchat.R;
import com.ja.openchat.databinding.TopnavigationbarBinding;

import me.goldze.mvvmhabit.utils.KLog;


/**
 * 对顶部导航栏的定义
 */
public class TopNavigationBar extends Toolbar {

    TopnavigationbarBinding binding;

    public TopNavigationBar(@NonNull final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setContentInsetsRelative(0, 0);
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.topnavigationbar, this, true);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopNavigationBar);
        Drawable drawable = typedArray.getDrawable(R.styleable.TopNavigationBar_topnavigation_back);
        Drawable drawableRight = typedArray.getDrawable(R.styleable.TopNavigationBar_topnavigation_icon_right);
        String title = typedArray.getString(R.styleable.TopNavigationBar_topnavigation_title);
        String right = typedArray.getString(R.styleable.TopNavigationBar_topnavigation_right);
        int color = typedArray.getColor(R.styleable.TopNavigationBar_topnavigation_right_color, getResources().getColor(R.color.color_text));
        int colorTitle = typedArray.getColor(R.styleable.TopNavigationBar_topnavigation_title_color, getResources().getColor(R.color.color_text));

        typedArray.recycle();
        binding.ivBack.setOnClickListener(view -> ((Activity) context).finish());
        binding.tvTitle.setText(title);
        binding.tvNavigationRight.setText(right);
        try {
            if (drawable != null)
                binding.ivBack.setImageDrawable(drawable);
            if (drawableRight != null) {
                binding.ivRight.setImageDrawable(drawableRight);
                binding.ivRight.setVisibility(VISIBLE);
            } else {
                binding.ivRight.setVisibility(GONE);
            }
            binding.tvTitle.setTextColor(colorTitle);
            binding.tvNavigationRight.setTextColor(color);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setTitleAlpha(float alpha) {
        binding.tvTitle.setAlpha(alpha);
    }

    public void setBackVisibility(boolean show) {
        binding.ivBack.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
    }

    public void setOnBackListener(OnClickListener listener) {
        binding.ivBack.setOnClickListener(listener);
    }

    public void setOnIconRightListener(OnClickListener listener) {
        binding.ivRight.setOnClickListener(listener);
    }

    public void setRightIconVisibility(boolean show) {
        binding.ivRight.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void setRightIcon(Drawable drawable) {
        if (drawable == null) {
            binding.ivRight.setVisibility(GONE);
        } else {
            binding.ivRight.setVisibility(VISIBLE);
            binding.ivRight.setImageDrawable(drawable);
        }

    }

    public void setOnRightListener(OnClickListener listener) {
        binding.tvNavigationRight.setOnClickListener(listener);
    }

    public void setTitle(String title) {
        binding.tvTitle.setText(title);
    }

    public void setOnRightBgListener(OnClickListener listener) {
        binding.tvNavigationRight.setTextColor(getResources().getColor(R.color.white));
        binding.tvNavigationRight.setBackgroundResource(R.drawable.found_circle_publish_btn_bg);
        binding.tvNavigationRight.setPadding(SizeUtils.dp2px(12), SizeUtils.dp2px(4), SizeUtils.dp2px(12), SizeUtils.dp2px(4));
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
        layoutParams.rightMargin = SizeUtils.dp2px(15);
        binding.tvNavigationRight.setLayoutParams(layoutParams);
        binding.tvNavigationRight.setOnClickListener(listener);
    }

    @BindingAdapter(value = {"topnavigation_icon_right"})
    public static void setTopnavigationIconRight(TopNavigationBar bar, Drawable icon) {
        KLog.d("setTopnavigationIconRight" + icon);
        bar.setRightIcon(icon);
    }

    @BindingAdapter(value = {"topnavigation_title"})
    public static void setTopnavigationTitle(TopNavigationBar bar, String title) {
        KLog.d("topnavigation_title" + title);
        bar.setTitle(title);
    }
}
